package Admin;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import javax.swing.JOptionPane;

import GroupClass.ConnectRMI;
import Project_etow.EtowInterface;

public class Insert_Town extends javax.swing.JDialog {

    /**
     * Creates new form TownName
     */
    public Insert_Town(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    
    private String checkvisable;
	
	public String getCheckvisable() {
		return checkvisable;
	}

	public void setCheckvisable(String checkvisable) {
		this.checkvisable = checkvisable;
	}

	ConnectRMI connectRMI = new ConnectRMI();
    
    @SuppressWarnings("unchecked")
   
    private void initComponents() {

        btnSubmit = new javax.swing.JButton();
        btnReset = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        txtFTownName = new javax.swing.JTextField();
        txtFNumberFloor = new javax.swing.JTextField();       
        lblTownName = new javax.swing.JLabel();
        lblNumberFloor = new javax.swing.JLabel();        
        
        setTitle("Insert New Town");
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        btnSubmit.setText("Submit");
        btnSubmit.setActionCommand("Submit");
        btnSubmit.addActionListener(new ActionListener() {			
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					try {
					    Registry registry = LocateRegistry.getRegistry(connectRMI.host,connectRMI.port);
	                                        EtowInterface obj=(EtowInterface)registry.lookup(connectRMI.regis);
						
						String townName   = txtFTownName.getText();
						String numfloortown = txtFNumberFloor.getText();
						String statustown 	  = "0";
						
						if (townName.length() == 0) {
							JOptionPane.showMessageDialog(null, "TownName Not Null");
						}
						else if (numfloortown.length() == 0) {
							JOptionPane.showMessageDialog(null, "Number Floor Not Null");
						}
						else if(statustown.length() == 0){
							JOptionPane.showMessageDialog(null, "Status Not Null");
						}						
						else if (townName.length() != 0 && numfloortown.length() != 0 && statustown.length() != 0){
						    String result = obj.InsertTown(townName,numfloortown, statustown);
						    if (result.equals("Insert")) {
								JOptionPane.showMessageDialog(null, "Insert Successful");								
								setCheckvisable("close");
								setVisible(false);
							}
							else {
								JOptionPane.showMessageDialog(null, "Insert Failed");
							}
						}
						
						}catch (Exception ex) { 
						ex.printStackTrace();
						}							
		}
	});

        btnReset.setText("Reset");
		btnReset.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				txtFTownName.setText("");				
				txtFNumberFloor.setText("");
			}
		});

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				setCheckvisable("close");
				setVisible(false);
			}
		});

        txtFTownName.setText("");

        txtFNumberFloor.setText("");

        lblTownName.setText("Town Name");

        lblNumberFloor.setText("Number  Floor");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblNumberFloor)
                            .addComponent(lblTownName)
                           )
                        .addGap(16, 16, 16)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtFNumberFloor, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 141, Short.MAX_VALUE)
                            .addComponent(txtFTownName, javax.swing.GroupLayout.Alignment.TRAILING)
                            ))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnSubmit)
                        .addGap(21, 21, 21)
                        .addComponent(btnReset)
                        .addGap(18, 18, 18)
                        .addComponent(btnCancel)))
                .addContainerGap(28, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblTownName)
                    .addComponent(txtFTownName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblNumberFloor)
                    .addComponent(txtFNumberFloor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
)
                   
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSubmit)
                    .addComponent(btnReset)
                    .addComponent(btnCancel))
                .addGap(29, 29, 29))
        );

        btnSubmit.getAccessibleContext().setAccessibleName("");

        pack();
    }// </editor-fold>


	/**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Insert_Town.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Insert_Town.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Insert_Town.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Insert_Town.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the dialog
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
            	Insert_Town dialog = new Insert_Town(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnReset;
    private javax.swing.JButton btnSubmit;
    private javax.swing.JLabel lblNumberFloor;
    private javax.swing.JLabel lblTownName;
    private javax.swing.JTextField txtFNumberFloor;
    private javax.swing.JTextField txtFTownName;
    
    
    // End of variables declaration
}
